const { formatTime } = require("../../extend/helper.js");
const { format } = require("./time.js");
const array = require("./array.js");
const time = require("./time.js");
const string=require("./string.js");
const source = require("../../public/source.js");
module.exports = {
    //获取随机字符串————根据多个数组
    //参数是n个一维数组[],[],[],...
    //如果有最后一个areaStr,例如："[1,4]","(1,4]"等，将会得到这个指定范围的数组个数的随机字符串的拼接值
    strJoinFromArrs() {
        let str = "";
        let args = Array.from(arguments);
        if (source.regexp.areaStr.test(args.slice(-1))) {
            args = this.arrOfArr(args.slice(0, -1), args.slice(-1)[0]);
        }
        for (let i = 0; i < args.length; i++) {
            let item = args[i];
            if (source.regexp.areaStr.test(item)) {    //判断是否是区间字符串   '[1,2]'为true， [1,2]为false
                item = array.byArea(item);
            }
            let probability = 1 / item.length;  //概率
            let ind = Math.floor(Math.random() / probability);
            str += item[ind];
        }
        return str;
    },
    //根据最大值获取随机数
    //type: ceil 或 floor
    numByMax(max, type = "floor") {
        return Math[type](Math.random() * max);
    },
    //从区间数组中获取随机数
    //area:  示例 (0,10]
    numByArea(areaStr) {
        let num = 0;
        let { leftBracket, rightBracket, min, max }=string.resolveAreaStr(areaStr);
        // areaStr = areaStr.replace(/\s/g, "");
        // let areaArr = areaStr.replace(/[\[\]\(\)]/g, "").split(",");
        // let min = parseInt(areaArr[0]);
        // let max = parseInt(areaArr[1]);
        // let leftBracket = areaStr.charAt(0);
        // let rightBracket = areaStr.charAt(areaStr.length - 1);
        switch (true) {
            case leftBracket == "(" && rightBracket == ")":
                num = min + Math.ceil(Math.random() * (max - min - 1));
                break;
            case leftBracket == "(" && rightBracket == "]":
                num = min + Math.ceil(Math.random() * (max - min));
                break;
            case leftBracket == "[" && rightBracket == ")":
                num = min + Math.floor(Math.random() * (max - min));
                break;
            case leftBracket == "[" && rightBracket == "]":
                num = min + Math.floor(Math.random() * (max - min + 1));
                break;
            default: break;
        }
        return num;
    },
    //获取数组的随机下标
    indOfArr(arr) {
        let ind = this.numByMax(arr.length);
        return ind;
    },
    //从数组中获取随机数
    eleOfArr(arr) {
        let ind = Math.floor(Math.random() * arr.length);
        return arr[ind];
    },
    indAndEleOfArr(arr) {
        let ind = this.numByMax(arr.length);
        let ele = arr[ind];
        return {
            ind,
            ele
        };
    },
    //根据权重获取随机数
    //示例：weightArr=[[90,[1]],[4,[2]],[3,[3]],[2,[4]],[1,[5,6,7,8,9,10]]]   注意:90+4+3+3+1=100
    numByWeight(weightArr) {
        let num = weightArr[0][1][0], count = 0;
        let random = Math.floor(Math.random() * 100);
        for (let i = 0; i < weightArr.length; i++) {
            count += weightArr[i][0]
            if (random < count) {
                if(source.regexp.areaStr.test(weightArr[i][1])){
                    weightArr[i][1]=array.byArea(weightArr[i][1]);
                }
                num = this.eleOfArr(weightArr[i][1]);
                break;
            }
        }
        return num;
    },
    //根据时间范围获取随机时间
    timeByArea(sTime, eTime = "now") {
        sTime = time.format(sTime);
        eTime = time.format(eTime);
        let sMs = new Date(sTime).getTime();
        let eMs = new Date(eTime).getTime();
        if (sMs > format(eMs)) {
            let tMs = sMs;
            sMs = eMs;
            eMs = tMs;
        }
        let diffMs = eMs - sMs;
        let randMs = Math.floor(Math.random() * diffMs);
        return format(new Date(sMs + randMs));
    },
    // //根据时间段获取随机时间
    // timeByLong(oldTime,longTime){
    //     oldTime=time.format(oldTime);
    //     longTime=time.format(longTime);
    //     return this.timeByArea(oldTime,longTime);
    // },
    //获取数组的子数组
    // area  [1,2,3,4]或"[1,4]" 或 4
    arrOfArr(arr, area=`[0,${arr.length}]`) {
        let num = 0, subArr = [];
        switch (typeof area) {
            case source.regexp.areaStr.test(area): num = this.numByArea(area); break;
            case source.regexp.weightStr.test(area): num = this.numByWeight(string.toWeightArr(area)); break;
            case "number": num = area; break;
            case "object": num = this.eleOfArr(area); break;
        }
        for (let i = 0; i < num; i++) {
            let ind = this.numByMax(arr.length);
            subArr.push(arr.splice(ind, 1)[0])
        }
        return subArr;
    }
}
